﻿﻿//------------------------------------------------------------------------------
// <copyright file="Common.cs" company="Microsoft">
//
//      Copyright 2013 Microsoft Corporation 
//
//      Licensed under the Microsoft Public License (Ms-PL). 
//      You may not use this file except in compliance with the License.
//
//      Unless required by applicable law or agreed to in writing, software 
//      distributed under the License is distributed on an "AS IS" BASIS,
//      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
//      See the License for the specific language governing permissions and 
//      limitations under the License. 
//
// </copyright>
﻿//------------------------------------------------------------------------------

namespace OpenXmlHelper
{
    using System;
    using System.Linq;
    using DocumentFormat.OpenXml;
    using DocumentFormat.OpenXml.Spreadsheet;

    public static class Common
    {
        public static bool AppendChild(Type childType, OpenXmlElement child, Worksheet worksheet)
        {
            bool added = false;
            for (var i = 0; i < WorksheetChildOrder.Length; i++)
            {
                if (WorksheetChildOrder[i] == childType)
                {
                    for (var j = i - 1; j >= 0; j--)
                    {
                        var previousItem = worksheet.ChildElements.FirstOrDefault(t => t.GetType() == WorksheetChildOrder[j]);
                        if (previousItem != null)
                        {
                            previousItem.InsertAfterSelf(child);
                            added = true;
                            break;
                        }
                    }

                    if (!added)
                    {
                        worksheet.InsertAt(child, 0);
                        added = true;
                    }
                }
            }

            return added;
        }

        public static readonly Type[] WorksheetChildOrder = new[]
        {
            typeof (SheetCalculationProperties),
            typeof (WorksheetExtensionList),
            typeof (SheetProperties),
            typeof (SheetDimension),
            typeof (SheetViews),
            typeof (SheetFormatProperties),
            typeof (Columns),
            typeof (SheetData),
            typeof (SheetProtection),
            typeof (ProtectedRanges),
            typeof (Scenarios),
            typeof (AutoFilter),
            typeof (SortState),
            typeof (DataConsolidate),
            typeof (CustomSheetViews),
            typeof (MergeCells),
            typeof (PhoneticProperties),
            typeof (ConditionalFormatting),
            typeof (DataValidations),
            typeof (Hyperlinks),
            typeof (PrintOptions),
            typeof (PageMargins),
            typeof (PageSetup),
            typeof (HeaderFooter),
            typeof (RowBreaks),
            typeof (ColumnBreaks),
            typeof (CustomProperties),
            typeof (CellWatches),
            typeof (IgnoredErrors),
            typeof (Drawing),
            typeof (LegacyDrawing),
            typeof (LegacyDrawingHeaderFooter),
            typeof (DrawingHeaderFooter),
            typeof (Picture),
            typeof (OleObjects),
            typeof (Controls),
            typeof (WebPublishItems),
            typeof (TableParts)
        };
    }
}